package com.qaml.mes.device.repository.impl;

import cn.hutool.core.date.DatePattern;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.qaml.mes.device.dao.HolidayApplyDO;
import com.qaml.mes.device.dao.mapper.HolidayApplyMapper;
import com.qaml.mes.device.repository.HolidayApplyRepository;
import com.qaml.mes.device.utils.DateUtils;
import com.qaml.mes.device.dto.eqp.HolidayApplyDTO;
import com.qaml.mes.device.enums.CheckUpkeepTypeEnum;
import com.qaml.mes.device.enums.HolidayApplyEnum;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @ClassName HolidayApplyRepositoryImpl
 * @Description TODO
 * @Author leikai
 * @Date 2024/3/4 0004 11:27
 * @Version 1.0
 */
@Repository
public class HolidayApplyRepositoryImpl implements HolidayApplyRepository {

    @Resource
    private HolidayApplyMapper holidayApplyMapper;

    /**
     * 创建假期申请单
     *
     * @param holidayApplyDO
     */
    @Override
    public void createHolidayApply(HolidayApplyDO holidayApplyDO) {
        holidayApplyMapper.insert(holidayApplyDO);
    }

    @Override
    public List<HolidayApplyDO> selectByDateAndType(Date startDateByMonth, Date endDateByMonth, String type) {
        LambdaQueryWrapper<HolidayApplyDO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.ge(HolidayApplyDO::getStartTime, startDateByMonth);
        queryWrapper.le(HolidayApplyDO::getEndTime, endDateByMonth);
        queryWrapper.eq(HolidayApplyDO::getType, type);
        queryWrapper.eq(HolidayApplyDO::getIsDeleted, 0);
        queryWrapper.eq(HolidayApplyDO::getStatus, 1);
        return holidayApplyMapper.selectList(queryWrapper);
    }

    /**
     * 通过时间获取休假详情
     *
     * @param holidayApplyDTO
     * @return
     */
    @Override
    public HolidayApplyDO getHolidayApply(HolidayApplyDTO holidayApplyDTO) {

        // 根据类型以及检保日期 获取开始时间以及结束时间，来进行匹配，
        if (holidayApplyDTO.getType().equals(CheckUpkeepTypeEnum.CHECK.getType())) {
            holidayApplyDTO.setStartTime(DateUtils.getStartDate(holidayApplyDTO.getCheckUpkeepDate()));
            holidayApplyDTO.setEndTime(DateUtils.getEndDate(holidayApplyDTO.getCheckUpkeepDate()));
        } else if (holidayApplyDTO.getType().equals(CheckUpkeepTypeEnum.UPKEEP.getType())) {
            String date = DateUtils.format(holidayApplyDTO.getCheckUpkeepDate(), DatePattern.NORM_DATETIME_PATTERN);
            holidayApplyDTO.setStartTime(DateUtils.getStartDateByMonth(date));
            holidayApplyDTO.setEndTime(DateUtils.getEndDateByMonth(date));
        }

        LambdaQueryWrapper<HolidayApplyDO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(HolidayApplyDO::getIsDeleted, 0);
        queryWrapper.eq(HolidayApplyDO::getType, holidayApplyDTO.getType());
        queryWrapper.ge(HolidayApplyDO::getStartTime, holidayApplyDTO.getStartTime());
        queryWrapper.le(HolidayApplyDO::getEndTime, holidayApplyDTO.getEndTime());
        queryWrapper.ne(HolidayApplyDO::getStatus, HolidayApplyEnum.REJECTED.getType());
        return holidayApplyMapper.selectOne(queryWrapper);
    }

    /**
     * 通过id获取休假申请
     *
     * @param id
     * @return
     */
    @Override
    public HolidayApplyDO getById(String id) {
        LambdaQueryWrapper<HolidayApplyDO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(HolidayApplyDO::getId, id);
        queryWrapper.eq(HolidayApplyDO::getIsDeleted, 0);
        return holidayApplyMapper.selectOne(queryWrapper);
    }

    /**
     * 更新休假申请
     *
     * @param holidayApplyDO
     */
    @Override
    public void updateById(HolidayApplyDO holidayApplyDO) {
        holidayApplyMapper.updateById(holidayApplyDO);
    }
}